

#include "tools/socket.h"

#include <string>
#include <sstream>
#include <stdio.h>

int main()
{
#if 0
    // {{{
    tools::CSocket  sockL(tools::CSocket::eSocketType_TCP);
    tools::CSocket  sockT(tools::CSocket::eSocketType_TCP);

    sockL.Create();

    sockL.Bind(10234);
    sockL.Listen();
    sockL.Accept(sockT);

    char    buffer[4096];
    int ret = sockT.Recv(buffer, sizeof(buffer));
    buffer[ret] = '\0';
    printf("------------------ recv -------------------\n");
    printf("%s\n", buffer);

    
    std::stringstream   oss;
    std::string str;
    str = "{\"errorcode\":\"100\", \"hbInterval\":\"60\", \"hbip\":\"10.0.0.1\", \"hbport\": 10244, \"msg\":\"kazuki's false urg.\"}";
    oss << "HTTP/1.1 200 OK\r\n";
    oss << "Server: kazuki false\r\n";
    oss << "Content-Type: text/html;charset=utf-8\r\n";
    oss << "Content-Length: " << str.length() << "\r\n";
    oss << "\r\n\r\n";
    oss << str;

    sockT.Send(oss.str().c_str(), oss.str().length());
    printf("---------------- send -----------------\n");
    printf("%s\n", oss.str().c_str());

    ret = sockT.Recv(buffer, sizeof(buffer));
    buffer[ret] = '\0';
    printf("------------------ recv -------------------\n");
    printf("%s\n", buffer);
    // }}}
#endif

    tools::CSocket  sockU(tools::CSocket::eSocketType_UDP);
    sockU.Create();
    sockU.Bind(10244);

    while (true) {
        char    buffer[4096];
        int ret = sockU.Recv(buffer, sizeof(buffer));
        buffer[ret] = '\0';
        printf("------------------ recv -------------------\n");
        printf("%s\n", buffer);
    }

    return 0;
}

